#define F_CPU 1000000UL //Defines clock speed
#include <avr/io.h>
#include <avr/interrupt.h>
#define USART_BAUDRATE 4800 //Baudrate for serial comm.
//#define BAUD_PRESCALE 95 //95 sad in Atmega16 datasheet for 14745600UL oscil
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

//Serial com. Interrupt Service Routine (runs each time a byte is received)
ISR(USART_RXC_vect)
{
    char ReceivedByte;
    ReceivedByte = UDR; // Retrieves byte from serial port (bluetooth module)
    UDR = ReceivedByte; // Echoes it back for fun
 
    switch (ReceivedByte) //Which ASCII character was received?
    {
 
        case 'p':   PORTB =  0b00000100;//l
                    break; 
					 
        case 'n':   PORTB =  0b00000010;//k
                    break; 

        case 'r':   PORTB =  0;//m
                    break; 
 
        case 'l':   PORTB =  0b00000001;//j
                    break; 
 
        default:    break;
 
    }
 
}
 
int main(void)
{
    //I/O Initialization
    DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs
    //PORTB=0; //All initialized to 0
 
    //UART
    UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx
    UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
	UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers
    UBRRL = BAUD_PRESCALE;//Sets
    UCSRB |= (1 << RXCIE); //Enable USART-interrupt
 
    sei(); //Enable global interrupt
 
    /* This program is completely interrupt driven, so nothing goes on in while loop*/
    while(1); //Never gets out from here!
 
    return 0; //Never reaches this point!
}